﻿

//声明了个控制权名称：common.views.caseCategorys.index
(function () {
    appModule.controller('common.views.caseCategorys.index', [
      '$scope', '$uibModal', '$stateParams', 'uiGridConstants', 'abp.services.app.caseCategory',
        function ($scope, $uibModal, $stateParams, uiGridConstants, caseCategoryService) {
            //$stateParams 获取的是浏览器后面跟的参数
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                //这里应该是当页面加载完毕后，进行信息的初始化
                //实际会去调用 icheck、select2等js的初始化插件。来渲染页面
                App.initAjax();
            });
            //告知页面信息已经下载完毕
            vm.loading = false;

            //获取Session中的userId
            //   vm.currentUserId = abp.session.userId;
            //制作权限组，进行页面权限的判断 
            vm.permissions = {
                create: abp.auth.hasPermission("Pages.CaseCategory.CreateCaseCategory"),
                edit: abp.auth.hasPermission("Pages.CaseCategory.EditCaseCategory"),
                'delete': abp.auth.hasPermission("Pages.CaseCategory.DeleteCaseCategory"),
                del: abp.auth.hasPermission("Pages.CaseCategory.DeleteCaseCategory"),
            };

            //导出为excel文档
            vm.exportToExcel = function () {
                caseCategoryService.getCaseCategoryToExcel({})
                      .then(function (result) {
                          app.downloadTempFile(result.data);
                      });
            };

            vm.caseCategoryTree = {

                $tree: null,

                selectedOu: {
                    id: null,
                    displayName: null,
                    code: null,

                    set: function (ouInTree) {
                        if (!ouInTree) {
                            vm.caseCategoryTree.selectedOu.id = null;
                            vm.caseCategoryTree.selectedOu.displayName = null;
                            vm.caseCategoryTree.selectedOu.code = null;
                        } else {
                            vm.caseCategoryTree.selectedOu.id = ouInTree.id;
                            vm.caseCategoryTree.selectedOu.displayName = ouInTree.original.displayName;
                            vm.caseCategoryTree.selectedOu.code = ouInTree.original.code;
                        }

                        vm.members.load();
                    }
                },

                contextMenu: function (node) {

                    var items = {
                        editUnit: {
                            label: app.localize('Edit'),
                            _disabled: !vm.permissions.edit,
                            action: function (data) {
                                var instance = $.jstree.reference(data.reference);

                                vm.caseCategoryTree.openCreateOrEditUnitModal(node.id);
                            }
                        },

                        'delete': {
                            label: app.localize("Delete"),
                            _disabled: !vm.permissions.del,
                            action: function (data) {
                                var instance = $.jstree.reference(data.reference);

                                var currentTree = vm.caseCategoryTree.$tree.jstree('get_node', node.id);
                                if (currentTree.children.length > 0) {
                                    abp.message.confirm(
                                        app.localize('请先删除' + currentTree.original.title + '的子案例！'),
                                        function (isConfirmed) {
                                            if (isConfirmed) {
                                                return;
                                            }
                                        }
                                    );
                                }
                                else {
                                    abp.message.confirm(
                                        app.localize('AreaDeleteWarningMessage', currentTree.original.title),
                                        function (isConfirmed) {
                                            if (isConfirmed) {
                                                caseCategoryService.deleteCaseCategoryAsync({
                                                    id: node.id
                                                }).then(function () {
                                                    vm.caseCategoryTree.reload();
                                                    abp.notify.success(app.localize('SuccessfullyDeleted'));
                                                });
                                            }
                                        }
                                    );
                                }

                            }
                        }
                    }

                    return items;
                },

                openCreateOrEditUnitModal: function (caseCategoryId, closeCallback) {
                    var modalInstance = $uibModal.open({
                        size: 'lg',
                        animation: true,
                        templateUrl: '~/App/common/views/caseCategorys/createOrEditModal.cshtml',
                        controller: 'common.views.caseCategorys.createOrEditModal as vm',
                        backdrop: 'static',
                        resolve: {
                            caseCategoryId: function () {
                                return caseCategoryId;
                            }
                        }
                    });

                    modalInstance.result.then(function (result) {
                        closeCallback && closeCallback(result);
                        vm.caseCategoryTree.reload();
                    });
                },

                generateTextOnTree: function (ou) {
                    return '<span title="' + ou.title + '" class="ou-text ou-text-no-members" data-ou-id="' + ou.id + '">' + ou.title + ' <i class="fa fa-caret-down text-muted"></i></span>';
                },

                getTreeDataFromServer: function (callback) {
                    caseCategoryService.getCaseCategorys().then(function (result) {
                        var treeData = _.map(result.data.items, function (item) {
                            return {
                                id: item.id,
                                parent: item.parentID ? item.parentID : '#',
                                code: item.id,
                                title: item.title,
                                text: vm.caseCategoryTree.generateTextOnTree(item),
                                state: {
                                    opened: false
                                }
                            };
                        });

                        callback(treeData);
                    });
                },

                init: function () {
                    vm.caseCategoryTree.getTreeDataFromServer(function (treeData) {

                        vm.caseCategoryTree.$tree = $('#caseCategoryTree');

                        var jsTreePlugins = [
                            'types',
                            'contextmenu',
                            'wholerow',
                            'sort'
                        ];

                        vm.caseCategoryTree.$tree
                            .jstree({
                                'core': {
                                    data: treeData,
                                    multiple: false,
                                    check_callback: function (operation, node, node_parent, node_position, more) {
                                        return true;
                                    }
                                },
                                types: {
                                    "default": {
                                        "icon": "fa fa-folder tree-item-icon-color icon-lg"
                                    },
                                    "file": {
                                        "icon": "fa fa-file tree-item-icon-color icon-lg"
                                    }
                                },
                                contextmenu: {
                                    items: vm.caseCategoryTree.contextMenu
                                },
                                sort: function (node1, node2) {
                                    if (this.get_node(node2).original.displayName < this.get_node(node1).original.displayName) {
                                        return 1;
                                    }

                                    return -1;
                                },
                                plugins: jsTreePlugins
                            });

                        vm.caseCategoryTree.$tree.on('click', '.ou-text .fa-caret-down', function (e) {
                            e.preventDefault();

                            var ouId = $(this).closest('.ou-text').attr('data-ou-id');
                            setTimeout(function () {
                                vm.caseCategoryTree.$tree.jstree('show_contextmenu', ouId);
                            }, 100);
                        });
                    });
                },

                reload: function () {
                    vm.caseCategoryTree.getTreeDataFromServer(function (treeData) {
                        vm.caseCategoryTree.$tree.jstree(true).settings.core.data = treeData;
                        vm.caseCategoryTree.$tree.jstree('refresh');
                    });
                }

            };

            //初始化Tree
            vm.caseCategoryTree.init();
        }
    ]);
})();











